def day_of_year(year, month, day):
    """
    判断给定的某年某月某日是这一年的第几天
    """
    months_days = [0, 31, 28, 31, 32, 31, 30, 31, 31, 30, 31, 30, 31]
    if (year % 4 == 0 and year % 100!= 0) or year % 400 == 0:
        months_days[2] = 29
    total_days = 0
    for m in range(1, month):
        total_days += months_days[m]
    return total_days + day

def count_primes_under_1000():
    """
    判断1000以内有多少个素数，并输出所有素数
    """
    primes = []
    for num in range(2, 1000):
        is_prime = True
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(num)
    return primes

def grade_to_letter(grade):
    """
    利用条件运算符的嵌套根据成绩给出对应等级
    """
    if grade >= 90:
        return 'A'
    elif grade >= 60:
        return 'B'
    return 'C'

def count_characters(s):
    """
    输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数
    """
    letters = 0
    spaces = 0
    digits = 0
    others = 0
    for char in s:
        if char.isalpha():
            letters += 1
        elif char.isspace():
            spaces += 1
        elif char.isdigit():
            digits += 1
        else:
            others += 1
    return letters, spaces, digits, others

def find_perfect_numbers():
    """
    找出1000以内的所有完数
    """
    perfect_numbers = []
    for num in range(1, 1000):
        factors = []
        for i in range(1, num):
            if num % i == 0:
                factors.append(i)
        if sum(factors) == num:
            perfect_numbers.append(num)
    return perfect_numbers

def factorial_sum(n):
    """
    求1+2!+3!+...+N!的值
    """
    factorial_sum_value = 0
    factorial = 1
    for i in range(1, n + 1):
        factorial *= i
        factorial_sum_value += factorial
    return factorial_sum_value

def print_diamond(n):
    """
    输入任意奇数N，按要求打印出菱形图案
    """
    for i in range(1, n + 1, 2):
        print(' ' * ((n - i) // 2) + '*' * i)
    for i in range(n - 2, 0, -2):
        print(' ' * ((n - i) // 2) + '*' * i)

def sort_ten_numbers():
    """
    对10个数进行排序
    """
    numbers = []
    for _ in range(10):
        num = float(input("请输入一个数字: "))
        numbers.append(num)
    numbers.sort()
    return numbers

def insert_into_sorted_array():
    """
    有一个已经排好序的数组，从键盘任意输入一个数，按原来的规律将它插入数组中
    """
    sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
    num = float(input("请输入要插入的数字: "))
    inserted = False
    for i in range(len(sorted_array)):
        if num < sorted_array[i]:
            sorted_array.insert(i, num)
            inserted = True
            break
    if not inserted:
        sorted_array.append(num)
    return sorted_array

def last_remaining(n):
    """
    有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位
    """
    people = list(range(1, n + 1))
    index = 0
    while len(people) > 1:
        index = (index + 2) % len(people)
        people.pop(index)
    return people[0]